from django.db.models import Count
from rest_framework.decorators import api_view

from mh.models import Article
from utils.response_utils import ResponseUtils


@api_view(['GET'])
def doTopicStats(request):
    '''按照话题对文章进行统计'''
    results = []
    category_counts = Article.objects.values('topic_id').annotate(count=Count('id'))
    for item in category_counts:
        results.append(f"topic_id: {item['topic_id']}, Count: {item['count']}")
    return ResponseUtils.success_response(data=results)
